package com.zbkj.crmeb.merchant.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.store.dto.HomeActivityDTO;
import com.zbkj.crmeb.store.model.HomeActivity;
import com.zbkj.crmeb.store.request.HomeActivityRequest;
import com.zbkj.crmeb.store.service.HomeActivityService;
import com.zbkj.crmeb.store.service.HomeStyleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 个性化首页
 * @Description: 
 * @author daigb
 * @date 2021-09-25
 */
@RestController
@RequestMapping("api/admin/mer/home/activity")
@Api(tags = "个性化首页--活动")
public class MerHomeActivityController extends AdminBaseController {
    @Autowired
    HomeActivityService homeActivityService;
    @Autowired
    HomeStyleService homeStyleService;

    /**
     * 列表
     * @Description:  
     * @author daigb 
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<List<HomeActivityDTO>> listEntity(HttpServletRequest request) {
        return CommonResult.success(homeActivityService.listEntity(getMerId(request)));
    }
    
    /**
     * 分页
     * @Description:  
     * @author daigb 
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "分页")
    @GetMapping("/page")
    public CommonResult<PageInfo<HomeActivityDTO>> pageEntity(HomeActivityRequest homeActivityRequest, HttpServletRequest request) {
    	return CommonResult.success(homeActivityService.pageEntity(getMerId(request), homeActivityRequest));
    }
    
    /**
     * 新增
     * @Description:  
     * @author daigb 
     * @param @param homeActivity
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping
    public CommonResult<Object> save(@RequestBody List<HomeActivityDTO> homeActivityDTOs, HttpServletRequest request) {
        return CommonResult.success(homeActivityService.saveEntity(getMerId(request), homeActivityDTOs));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
        HomeActivity homeActivityDB = homeActivityService.getOne(Wrappers.<HomeActivity>lambdaQuery()
                .eq(HomeActivity :: getMerId, getMerId(request))
                .eq(HomeActivity :: getId, id)
                .last("LIMIT 1")
                );
        if(null == homeActivityDB) {
            return CommonResult.validateFailed("记录不存在");
        }
        
        return CommonResult.success(homeActivityService.removeById(id));
    }
    
    /**
     * 根据id查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public CommonResult<Object> get(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
    	HomeActivity homeActivityDB = homeActivityService.getOne(Wrappers.<HomeActivity>lambdaQuery()
    			.eq(HomeActivity :: getMerId, getMerId(request))
    			.eq(HomeActivity :: getId, id)
    			.last("LIMIT 1")
    			);
    	if(null == homeActivityDB) {
    		return CommonResult.validateFailed("记录不存在");
    	}

        // 对象复制
        HomeActivityDTO dto = new HomeActivityDTO();
        BeanUtils.copyProperties(homeActivityDB, dto);
        dto.setHomeStryles(homeStyleService.listEntityByActivityId(homeActivityDB.getId()));
    	
    	return CommonResult.success(dto);
    }
    
}
